package com.xxjqr.rbac.controller;


import com.github.pagehelper.PageInfo;
import com.xxjqr.rbac.entity.dto.JobDto;
import com.xxjqr.rbac.entity.vo.JobVo;
import com.xxjqr.rbac.enums.CodeMsgEnum;
import com.xxjqr.rbac.exception.CommonException;
import com.xxjqr.rbac.service.TbJobService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author 丁昌江
 * @since 2021-03-02
 */
@RestController
@RequestMapping("job")
@AllArgsConstructor
public class TbJobController {

    private final TbJobService jobService;

    @GetMapping("getJobs")
    public PageInfo<JobVo> getJobs(String code, String name, Integer status) {
        return jobService.getJobs(code,name,status);
    }

    @GetMapping("getJobList")
    public List<JobVo> getJobList() {
        return jobService.getJobList();
    }

    @PutMapping("editJob")
    public String editJob(@RequestBody JobDto jobDto) {
        if (jobDto.getId() == null) {
            throw CommonException.generate(CodeMsgEnum.NOTFOUND);
        }
        if (jobService.putJob(jobDto)) {
            return "修改成功";
        }
        return "修改失败";
    }

    @DeleteMapping("delJob")
    public Boolean delJob(Long id) {
        return jobService.removeById(id);
    }

    @PostMapping("addJob")
    public String addJob(@RequestBody @Valid JobDto jobDto) {
        if (jobService.addJob(jobDto)) {
            return "添加岗位成功";
        }
        throw CommonException.generate(CodeMsgEnum.NOTFOUND);
    }
}
